
# Run analyses ------------------------------------------------------------


# lc1
lc1_el <- ols_main(
	outcome = "lc1",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, complier == TRUE),
	dosage = FALSE,
	dosage_indicator = FALSE)

lc1_el_pvals <- get_RI_pvals(
	outcome = "lc1",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, complier == TRUE),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")


lc1_ml <- ols_main(
	outcome = "lc1",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(ml, compliance == 1),
	dosage = FALSE,
	dosage_indicator = FALSE)

lc1_ml_pvals <- get_RI_pvals(
	outcome = "lc1",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(ml, compliance == 1),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")


# pta

pta_el <- ols_main(
	outcome = "pta",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, complier == TRUE),
	dosage = FALSE,
	dosage_indicator = FALSE)

pta_el_pvals <- get_RI_pvals(
	outcome = "pta",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, complier == TRUE),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")


pta_ml <- ols_main(
	outcome = "pta",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(ml, compliance == 1),
	dosage = FALSE,
	dosage_indicator = FALSE)

pta_ml_pvals <- get_RI_pvals(
	outcome = "pta",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(ml, compliance == 1),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")


# bring_up

bring_up_el <- ols_main(
	outcome = "bring_up",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, complier == TRUE),
	dosage = FALSE,
	dosage_indicator = FALSE)

bring_up_el_pvals <- get_RI_pvals(
	outcome = "bring_up",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, complier == TRUE),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")


bring_up_ml <- ols_main(
	outcome = "bring_up",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(ml, compliance == 1),
	dosage = FALSE,
	dosage_indicator = FALSE)

bring_up_ml_pvals <- get_RI_pvals(
	outcome = "bring_up",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(ml, compliance == 1),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")


# assemble


assemble_el <- ols_main(
	outcome = "assemble",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, complier == TRUE),
	dosage = FALSE,
	dosage_indicator = FALSE)

assemble_el_pvals <- get_RI_pvals(
	outcome = "assemble",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, complier == TRUE),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")


assemble_ml <- ols_main(
	outcome = "assemble",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(ml, compliance == 1),
	dosage = FALSE,
	dosage_indicator = FALSE)

assemble_ml_pvals <- get_RI_pvals(
	outcome = "assemble",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(ml, compliance == 1),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

# absenteeism_action (ml, el)
absenteeism_action_el <- ols_main(
	outcome = "absenteeism_action",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, complier == TRUE),
	dosage = FALSE,
	dosage_indicator = FALSE)

absenteeism_action_el_pvals <- get_RI_pvals(
	outcome = "absenteeism_action",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, complier == TRUE),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

absenteeism_action_ml <- ols_main(
	outcome = "absenteeism_action",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(ml, compliance == 1),
	dosage = FALSE,
	dosage_indicator = FALSE)

absenteeism_action_ml_pvals <- get_RI_pvals(
	outcome = "absenteeism_action",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(ml, compliance == 1),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")


# Make table --------------------------------------------------------------

#setup
control_means <- 
	c(
		"Control Mean",
		round(mean(ml$lc1[ml$absenteeism == 0 & ml$compliance == 1],na.rm = TRUE), 2),
		round(mean(el$lc1[el$absenteeism == 0 & el$complier == TRUE],na.rm = TRUE), 2),
		round(mean(ml$bring_up[ml$absenteeism == 0 & ml$compliance == 1],na.rm = TRUE), 2),
		round(mean(el$bring_up[el$absenteeism == 0 & el$complier ==TRUE],na.rm = TRUE), 2),
		round(mean(ml$pta[ml$absenteeism == 0 & ml$compliance == 1],na.rm = TRUE), 2),
		round(mean(el$pta[el$absenteeism == 0 & el$complier ==TRUE],na.rm = TRUE), 2),
		round(mean(ml$assemble[ml$absenteeism == 0 & ml$compliance == 1],na.rm = TRUE), 2),
		round(mean(el$assemble[el$absenteeism == 0 & el$complier ==TRUE],na.rm = TRUE), 2),
		round(mean(ml$absenteeism_action[ml$absenteeism == 0 & ml$compliance == 1],na.rm = TRUE), 2),
		round(mean(el$absenteeism_action[el$absenteeism == 0 & el$complier ==TRUE],na.rm = TRUE), 2)	)


pval_lines <- c(
	"RI $p$-values",
	round(lc1_ml_pvals$ri_pvals["absenteeism"],3),
	round(lc1_el_pvals$ri_pvals["absenteeism"],3),
	round(bring_up_ml_pvals$ri_pvals["absenteeism"],3),
	round(bring_up_el_pvals$ri_pvals["absenteeism"],3),
	round(pta_ml_pvals$ri_pvals["absenteeism"],3),
	round(pta_el_pvals$ri_pvals["absenteeism"],3),
	round(assemble_ml_pvals$ri_pvals["absenteeism"],3),
	round(assemble_el_pvals$ri_pvals["absenteeism"],3),
	round(absenteeism_action_ml_pvals$ri_pvals["absenteeism"],3), #note 3-digit p values 
	round(absenteeism_action_el_pvals$ri_pvals["absenteeism"],3))

hypothesis_lines <- c(
	"Hypothesis",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr"
)

#make tables
sink("03_tables/ABS_conative_attitudes_test_all_compliers.tex")
stargazer(
	... = list(
		lc1_ml$fit,
		lc1_el$fit,
		bring_up_ml$fit,
		bring_up_el$fit,
		pta_ml$fit,
		pta_el$fit,
		assemble_ml$fit,
		assemble_el$fit,
		absenteeism_action_ml$fit,
		absenteeism_action_el$fit	),
	type = "latex",
	p = list(
		lc1_ml_pvals$ri_pvals,
		lc1_el_pvals$ri_pvals,
		bring_up_ml_pvals$ri_pvals,
		bring_up_el_pvals$ri_pvals,
		pta_ml_pvals$ri_pvals,
		pta_el_pvals$ri_pvals,
		assemble_ml_pvals$ri_pvals,
		assemble_el_pvals$ri_pvals,
		absenteeism_action_ml_pvals$ri_pvals,
		absenteeism_action_el_pvals$ri_pvals	),
	se = list(
		lc1_ml$fit_summary[,"Std. Error"],
		lc1_el$fit_summary[,"Std. Error"],
		bring_up_ml$fit_summary[,"Std. Error"],
		bring_up_el$fit_summary[,"Std. Error"],
		pta_ml$fit_summary[,"Std. Error"],
		pta_el$fit_summary[,"Std. Error"],
		assemble_ml$fit_summary[,"Std. Error"],
		assemble_el$fit_summary[,"Std. Error"],
		absenteeism_action_ml$fit_summary[,"Std. Error"],
		absenteeism_action_el$fit_summary[,"Std. Error"]	),
	keep = "absenteeism",
	omit.stat = c("rsq","f","ser"),
	column.separate = c(2,2,2,2,2),
	column.labels = c("Involve LC1 Chair","Tell village","Use PTA","Assemble group","Index"),
	table.layout = "=cd#-t-as=n",
	dep.var.labels = c("Midline","Endline",
										 "Midline","Endline",
										 "Midline","Endline",
										 "Midline","Endline",
										 "Midline","Endline"
	),
	dep.var.labels.include = TRUE,
	no.space = T,
	omit = "block_id",
	add.lines = list(
		control_means,
		pval_lines,
		hypothesis_lines,
		c("Block FE",
			"Yes","Yes","Yes",
			"Yes","Yes","Yes", 
			"Yes", "Yes","Yes",
			"Yes")),
	notes.label = "",
	float = FALSE,multicolumn = F
	# style = "qje"
)
sink()
